package quiz.pack;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


/*
 * Created by Nicholas Christensen 2012
 * Yeah..... enjoy
 */
public class Main extends JPanel implements ActionListener{
	//State checking junk
	boolean selectfile = true;
	boolean inquestion = false;
	boolean correct = false;
	boolean incorrect= false;
	
	//Quiz Junk
	int questionum=0;
	int correctan=0;
	int incorrectan=0;
	int answer=0;
	
	JFileChooser filechooser = new JFileChooser();
	Quiz quiz = null;
    protected JTextField textField;
    public static JTextArea textArea;
    GridBagConstraints c;
    JScrollPane scrollPane;
    String question;
    JButton an1 = new JButton("ERROR:Answer not Loaded");
    JButton an2 = new JButton("ERROR:Answer not Loaded");
    JButton an3 = new JButton("ERROR:Answer not Loaded");
    JButton an4 = new JButton("ERROR:Answer not Loaded");
    String filepath=null;
    int correctanswer;
	public Main(){
	    super(new GridBagLayout());

		filechooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
	    textArea = new JTextArea(1, 2);
	    textArea.setEditable(false);
	    scrollPane = new JScrollPane(textArea);

	    //Add Components to this panel.
	     c = new GridBagConstraints();
	    c.gridwidth = GridBagConstraints.REMAINDER;

	    c.fill = GridBagConstraints.HORIZONTAL;


	    c.fill = GridBagConstraints.BOTH;
	    c.weightx = 1.0;
	    c.weighty = 1.0;
	    an1.addActionListener(this);
	    an2.addActionListener(this);
	    an3.addActionListener(this);
	    an4.addActionListener(this);
	    setcomp();

	}
	public void setcomp(){
		

	    if(selectfile){
	    	try
	        {
	            int nState = filechooser.showOpenDialog(null);
	            if (nState  == JFileChooser.APPROVE_OPTION) 
	            {
	                File SelectedFile = filechooser.getSelectedFile();
	                filepath= SelectedFile.getPath();
	                System.out.println(filepath);
	                selectfile = false;
	                IFileReader fileread = new IFileReader();
	                quiz = fileread.readquiz(filepath);
	                inquestion=true;
	               // setcomp();
	            } 
	            else
	            {
	             System.exit(0);
	            }
	        }
	        catch(Exception e)
	        {
	            JOptionPane.showMessageDialog(null, e.getMessage(), "FILE SELECTION ERROR", JOptionPane.ERROR_MESSAGE);
	            System.exit(0);
	        } 
	    }
	    //IF ENDS HERE
	    if(inquestion){
	    	add(textArea,c);
	    	add(scrollPane,c);
	    	
	    	add(an1,c);
	    	add(an2,c);
	    	add(an3,c);
	    	add(an4,c);
	    	textArea.setText(quiz.questions[questionum]); 
	    	an1.setText(quiz.answers[questionum*4]);
	    	an2.setText(quiz.answers[questionum*4+1]);
	    	an3.setText(quiz.answers[questionum*4+2]);
	    	an4.setText(quiz.answers[questionum*4+3]);

	    	System.out.print(questionum);
	    }


	}
	private void dologic(){
		
if(answer!=0){
    if(answer==quiz.corans[questionum]){
		correct=true;
		incorrect=false;
	}
	else{
		incorrect=true;
		correct=false;
	}
}

		if(incorrect){
			JOptionPane.showMessageDialog(null,"That is Incorrect, Correct Answer:"+quiz.answers[questionum*4+quiz.corans[questionum]+1]);
			incorrectan++;
		}
		if(correct){
			JOptionPane.showMessageDialog(null, "You are correct");
			correctan++;
		}
		

    	questionum++;
		if(questionum==quiz.questions.length){
			JOptionPane.showMessageDialog(null, "End of Quiz:"+"You have got "+correctan+" correct out of "+(correctan+incorrectan));
			System.exit(0);
		}
	}
	private static void createAndShowGUI() {
	    //Create and set up the window.
	    JFrame frame = new JFrame("Quizil");
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	    //Add contents to the window.
	    frame.add(new Main());

	    //Display the window.
	    frame.pack();
	    frame.setVisible(true);
	}
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		if(e.getSource()==an1){
			answer=1;
		}
		if(e.getSource()==an2){
			answer =2;
		}
		if(e.getSource()==an3){
			answer = 3;
		}
		if(e.getSource()==an4){
			answer = 4;
			System.out.print(quiz.corans.length);
		}
	    textArea.setCaretPosition(textArea.getDocument().getLength());
		dologic();
		setcomp();

	}
	public static void main(String[] args) {
	    //Schedule a job for the event dispatch thread:
	    //creating and showing this application's GUI.
	    javax.swing.SwingUtilities.invokeLater(new Runnable() {
	        public void run() {
	        	JOptionPane.showMessageDialog(null, "Please select a quiz file!\nCreated by Nicholas Christensen 2012\n TIP: Enlarge the quiz screen for longer questions.");
	            createAndShowGUI();
	        }
	    });
	}
}
